﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [ObjectEvent]
    public class FightCalComponentEvent : ObjectEvent<FightCalComponent>, IAwake
    {
        public void Awake()
        {
            Get().Awake();
            Get().Awake(null);
        }
    }
    /// <summary>
    /// 符卡对决时，伤害计算的组件
    /// </summary>
    public class FightCalComponent : BattleEventComponent
    {
        public override BattleEventType[] EventType { get; set; } = new BattleEventType[] {BattleEventType.FightStart };
        
        public override int SortIndex { get; set; } = 6;

        public DamageInfo damageInfo;

        public static FightCalComponent Single;

        public void Awake(object o)
        {
            Single = this;
        }

        public override async Task RunEvent(BattleEventType eventType, FightModel fightModel, params object[] param)
        {

            DamageInfo damageInfo = param[0] as DamageInfo;
            damageInfo.DamageValue = damageInfo.AttackCard.GetComponent<SpellCardComponent>().Power;
            if (damageInfo.DefenceCard != null) damageInfo.DamageValue -= damageInfo.DefenceCard.GetComponent<SpellCardComponent>().Power;
            Log.Debug("战斗伤害计算");
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.DamageCal, fightModel, damageInfo);
            if (damageInfo.DamageValue < 0) damageInfo.DamageValue = 0;
            int Damage = damageInfo.DamageValue;
            Log.Debug($"攻击方战力:{damageInfo.AttackCard.GetComponent<SpellCardComponent>().Power}" +
                $"防御方战力:{damageInfo.DefenceCard?.GetComponent<SpellCardComponent>().Power}" +
                $"造成了{ Damage}伤害");
            if (Damage>0)
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.AttackSuccess, fightModel, damageInfo);
            else
                await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.DefenceSuccess, fightModel, damageInfo);
            fightModel.GetDefencePlayer().ChangeHp(damageInfo);

            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FightEnd, fightModel, damageInfo);
        }

        public async void DontDefence(FightModel fightModel)
        {
            fightModel.BattleStatus = BattleStatus.Battle;
            Log.Debug($"开始战斗结算");
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FightStart, fightModel, damageInfo);
        }
    }
}
